#!/bin/bash
# This file is part of the jetson_stats package (https://github.com/rbonghi/jetson_stats or http://rnext.it).
# Copyright (c) 2019 Raffaello Bonghi.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


# Emulator for NVIDIA Jetson tegrastats

usage()
{
	if [ "$1" != "" ]; then
    	tput setaf 1
		echo "$1"
		tput sgr0
	fi
	
    echo "tegrastats, Emulator of NVIDIA Jetsons"
    echo "Usage:"
    echo "$0 [options]"
    echo "options,"
    echo "   -h|--help    | This help"
    echo "   --interval   | Frequency interval"
}

main()
{
    local INTERVAL=500
    
	# Decode all information from startup
    while [ -n "$1" ]; do
        case "$1" in
            --interval)
                INTERVAL=$2
                shift 1
                ;;
            -h|--help)
                usage
                exit 0
                ;;
            *)
                usage "[ERROR] Unknown option: $1"
                exit 1
            ;;
        esac
            shift 1
    done
    
    # Evaluate time
    INTERVAL=$(echo "scale=2; $INTERVAL/1000" | bc)
    
    while true; do 
        # Generate random value for GPU
        RAND_GPU=$(( $RANDOM % 100 + 0 ))
        # Run example output for tegrastats
        echo "RAM 4722/7844MB (lfb 1x512kB) CPU [55%@2035,14%@2034,27%@2034,55%@2035,47%@2035,45%@2035] SWAP 149/1024MB (cached 7MB) EMC_FREQ 2%@1866 GR3D_FREQ $RAND_GPU%@1300 APE 150 MTS fg 0% bg 0% BCPU@-45C MCPU@-45C GPU@-51C PLL@45C AO@47.5C Tboard@37C Tdiode@46.75C PMIC@100C thermal@46.4C VDD_IN 14025/14416 VDD_CPU 2209/2538 VDD_GPU 6854/6903 VDD_SOC 1371/1370 VDD_WIFI 19/19 NVENC 716 NVDEC 716 VDD_DDR 2702/2702"
        # Go to sleep for an interval
        sleep $INTERVAL
        # Other example Jetson Nano
        # echo "RAM 1180/3965MB (lfb 1x2MB) SWAP 149/1024MB (cached 7MB) IRAM 0/252kB(lfb 252kB) CPU [30%@1036,33%@1036,28%@1036,26%@1036] EMC_FREQ 10%@1600 GR3D_FREQ 0%@76 NVENC 716 NVDEC 716 APE 25 PLL@57C CPU@60.5C PMIC@100C GPU@59C AO@65C thermal@59.75C POM_5V_IN 4520/4680 POM_5V_GPU 0/0 POM_5V_CPU 1078/1205"
        # Go to sleep for an interval
        # sleep $INTERVAL
    done
}

main $@
exit 0

# EOF
